//
//  FriendCirclesVC.m
//  echo
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 Danny. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width / 414
#define HEIGHT [UIScreen mainScreen].bounds.size.height / 736

#import "FriendCirclesVC.h"
#import "CellForHome.h"
#import "ModelForHome.h"
#import "NetworkingHandler.h"
#import <MJRefresh/MJRefresh.h>
#import "MusicPlayerVC.h"
#import "SettingUpVC.h"
#import <DKNightVersion.h>


@interface FriendCirclesVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arrForData;
@property (nonatomic, retain)UIButton *buttonForSetting;
#pragma ** 不知道是不是有用
@property (nonatomic, copy) DKColorPicker dk_tintColorPicker;
@end

@implementation FriendCirclesVC

- (void)dealloc{
    [_arrForData release];
    [_buttonForSetting release];
    self.tableView.delegate = nil;
    [_tableView release];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"postModelForHome" object:nil];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.hidesBackButton = YES;
    self.navigationItem.title = @"echo好友圈";
    [self handleData];
    [self createTableView];
    [self handleRefresh];
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"postModelForHome" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        MusicPlayerVC *vc = [[MusicPlayerVC alloc]init];
        ModelForHome *model = [note.userInfo objectForKey:@"model"];
        if ([model.source class] != nil) {
            
            [vc passModelForHome:model];
        }
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }];
    
}

- (void)handleData{
    
    [NetworkingHandler networkingPostWithUrl:@"http://echosystem.kibey.com/user/feed" body:@"feed_version=1&v=9&page=1" dicForValue:@{@"Cookie":@"PHPSESSID=16i74t79ge4ttb4f9uc4e0jkd4", @"x-v":@"20160209", @"x-c":@"3"} completeHandle:^(id result) {
     
        NSArray *arr = [result objectForKey:@"result"];
        
        if (!self.arrForData) {
            
            self.arrForData = [NSMutableArray array];
        }
        
        for (NSDictionary *dic in arr) {
            NSDictionary *tempDic = [dic objectForKey:@"sound"];
            NSDictionary *tempDic2 = [dic objectForKey:@"publisher"];
            ModelForHome *model = [[ModelForHome alloc]init];
            [model setValuesForKeysWithDictionary:tempDic];
            model.nameForTop = [tempDic2 objectForKey:@"name"];
            model.avatar = [tempDic2 objectForKey:@"avatar"];
//            [model setValuesForKeysWithDictionary:tempDic2];
            model.content = [dic objectForKey:@"content"];
            [self.arrForData addObject:model];
            [model release];
        }
        [self.tableView reloadData];
    }];
}

- (void)createTableView{
    
    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    [self.view addSubview:self.tableView];
    [self.tableView release];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    // 分割线样式
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[CellForHome class] forCellReuseIdentifier:@"poolForHome"];
    
    self.buttonForSetting = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.navigationController.navigationBar addSubview:self.buttonForSetting];
    self.buttonForSetting.frame = CGRectMake(self.view.frame.size.width - 50 * WIDTH, 9 * HEIGHT, 30 * WIDTH, 30 * WIDTH);
    [self.buttonForSetting setBackgroundImage:[UIImage imageNamed:@"iconfont-geren"] forState:UIControlStateNormal];
    [self.buttonForSetting addTarget:self action:@selector(handleSetting) forControlEvents:UIControlEventTouchUpInside];

}

- (void)handleSetting{
    SettingUpVC *vc = [[SettingUpVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arrForData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CellForHome *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForHome"];

    ModelForHome *model = [self.arrForData objectAtIndex:indexPath.row];
    
    [cell passModel:model];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ModelForHome *model = [self.arrForData objectAtIndex:indexPath.row];

//    CellForHome *cell = [[[CellForHome alloc]init] autorelease];
    CellForHome *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForHome"];

    return [cell heightForCell:model];
}

- (void)handleRefresh{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            self.arrForData = [NSMutableArray array];
            
            [self handleData];
            
            [self.tableView reloadData];
            
            [self.tableView.mj_header endRefreshing];
            
        });
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (DKColorPicker)dk_tintColorPicker {
    return objc_getAssociatedObject(self, @selector(dk_tintColorPicker));
}

- (void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:13 / 256.f green:19 / 256.f blue:30 / 256.f alpha:1]);

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
        [self.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor],NSFontAttributeName : [UIFont fontWithName:@"Arial" size:17]}];

    } else {
        
        [DKNightVersionManager dawnComing];
        [self.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor],NSFontAttributeName : [UIFont fontWithName:@"Arial" size:17]}];
    }
    self.tabBarController.tabBar.dk_barTintColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:18 / 256.f green:29 / 256.f blue:50 / 256.f alpha:1]);
    
    [self.tableView reloadData];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
